package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblVideoTypeMySql;
import com.huabo.system.mysql.mapper.TblVideoTypeMySqlMapper;
import com.huabo.system.oracle.entity.TblVideoType;
import com.huabo.system.oracle.mapper.TblVideoTypeMapper;
import com.huabo.system.service.TblVideoTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("tblVideoTypeImpl")
public class TblVideoTypeImpl implements TblVideoTypeService {

    @Resource
    private TblVideoTypeMapper tblVideoTypeMapper;

    @Resource
    private TblVideoTypeMySqlMapper tblVideoTypeMySqlMapper;

    @Override
    public String save(TblVideoType tnt, String token, String staffId) {
        if (token == null) {
            return null;
        }
        TblStaffUtil staff = null;
        try {
            staff = DealUserToken.parseUserToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (tnt != null && tnt.getTypeId() == null) {
            // 新建
            tnt.setVersion(1);
            tnt.setTypename(tnt.getTypename().trim());
            tnt.setOrgid(Integer.parseInt(staff.getCurrentOrg().getOrgid().toString()));
            this.tblVideoTypeMapper.insertTblVideoType(tnt);
            return JsonBean.success("1");
        } else {
            // 修改
            tnt.setOrgid(Integer.parseInt(staff.getCurrentOrg().getOrgid().toString()));
            tnt.setTypename(tnt.getTypename().trim());
            tnt.setVersion(tnt.getVersion() + 1);
            this.tblVideoTypeMapper.updateTblVideoType(tnt);
            return JsonBean.success("2");
        }

    }

    @Override
    public String saveMySql(TblVideoTypeMySql tnt, String token, String staffId) {
        if (token == null) {
            return null;
        }
        TblStaffUtil staff = null;
        try {
            staff = DealUserToken.parseUserToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (tnt != null && tnt.getTypeId() == null) {
            // 新建
            tnt.setVersion(1);
            tnt.setTypename(tnt.getTypename().trim());
            tnt.setOrgid(Integer.parseInt(staff.getCurrentOrg().getOrgid().toString()));
            this.tblVideoTypeMySqlMapper.insertTblVideoType(tnt);
            return JsonBean.success("1");
        } else {
            // 修改
            tnt.setOrgid(Integer.parseInt(staff.getCurrentOrg().getOrgid().toString()));
            tnt.setTypename(tnt.getTypename().trim());
            tnt.setVersion(tnt.getVersion() + 1);
            this.tblVideoTypeMySqlMapper.updateTblVideoType(tnt);
            return JsonBean.success("2");
        }
    }

    @Override
    public TblVideoType findByName(String typename) {
        List<TblVideoType> list = tblVideoTypeMapper.findBytypename(typename);
        if (list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public TblVideoTypeMySql findByMySqlName(String typename) {
        List<TblVideoTypeMySql> list = tblVideoTypeMySqlMapper.findBytypename(typename);
        if (list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    @Override
    public Map<String, Object> findAll(String token, String staffId, Integer pageNumber, Integer pageSize) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                if (token == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                Integer orgid = staff.getCurrentOrg().getOrgid().intValue();
                PageInfo<TblVideoType> pageInfo = new PageInfo<TblVideoType>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblVideoTypeMapper.selectListByPageInfo(pageInfo, orgid));
                pageInfo.setTotalRecord(tblVideoTypeMapper.selectCountByPageInfo(orgid));
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                if (token == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                Integer orgid = staff.getCurrentOrg().getOrgid().intValue();
                PageInfo<TblVideoType> pageInfo = new PageInfo<TblVideoType>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblVideoTypeMapper.selectListByPageInfo(pageInfo, orgid));
                pageInfo.setTotalRecord(tblVideoTypeMapper.selectCountByPageInfo(orgid));
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> findByid(String selectid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            TblVideoType tblVideoType = tblVideoTypeMapper.findByid(selectid);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", tblVideoType);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            TblVideoType tblVideoType = tblVideoTypeMapper.findByid(selectid);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", tblVideoType);
            return resultMap;
        }
    }

    @Override
    public void deleteById(String typeId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblVideoTypeMapper.deleteByTypeId(typeId);
        } else {
            tblVideoTypeMapper.deleteByTypeId(typeId);
        }
    }

    @Override
    public Map<String, Object> findAll(String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                List<TblVideoType> list = tblVideoTypeMapper.selectListByVideoType(orgid);
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                List<TblVideoType> list = tblVideoTypeMapper.selectListByVideoType(orgid);
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultMap;
        }
    }

}
